package com.ruoyi.net.utils.autoIncrement;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GlobalIncrementService {
    @Autowired
    private RedisCache redisCache;

    /**
     * 获取下一个全局自增数字 VPWS
     *
     * @return 自增后的数字
     */
    public String getNextVPWSIncrementNumber() {
        // 使用 Redis 的 increment 方法进行原子性自增操作
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.INCR_KEY_VPWS);
        return (10 +incrementNumber) + "";
    }

    /**
     * 重置全局自增数字 VPWS
     */
    public void resetVPWSIncrementNumber() {
        // 删除 Redis 中的键，重新开始计数
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.INCR_KEY_VPWS);
    }

    /*********************************/
    // 出标签，入标签，16-768432
    public String getNextOutLabelIncrementNumber() throws RuntimeException {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.INCR_KEY_OUT_LABEL);
        if (15 + incrementNumber.intValue() > 768432) {
            throw new RuntimeException("OutLabel自增数字超出范围[16,768432]");
        }
        return (15 + incrementNumber.intValue()) + "";
    }

    public String getNextInLabelIncrementNumber() throws RuntimeException {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.INCR_KEY_IN_LABEL);
        if (15 + incrementNumber.intValue() > 768432) {
            throw new RuntimeException("InLabel自增数字超出范围[16,768432]");
        }
        return (15 + incrementNumber.intValue()) + "";
    }

    public void resetOutLabelIncrementNumber() {
        // 删除 Redis 中的键，重新开始计数
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.INCR_KEY_OUT_LABEL);
    }

    public void resetInLabelIncrementNumber() {
        // 删除 Redis 中的键，重新开始计数
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.INCR_KEY_IN_LABEL);
    }

    /*********************************/
    // 从10位数开始， l2vpn 业务名称
    public String getNextPwClassNameIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.INCR_KEY_PW_CLASS_NAME);
        return (1000000000 + incrementNumber.intValue()) + "";
    }

    public void resetPwClassNameIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.INCR_KEY_PW_CLASS_NAME);
    }
    // 从10位数开始，  隧道策略业务名称
    public String getNextPwTnlPlcyIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.INCR_KEY_PW_TNL_PLCY);
        return (1000000000 + incrementNumber.intValue()) + "";
    }

    public void resetPwTnlPlcyIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.INCR_KEY_PW_TNL_PLCY);
    }
    // 从16开始，  隧道策略业务名称
    public String getNextTunnelIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.TUNNEL_ID);
        return 15+incrementNumber + "";
    }

    public void resetTunnelIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.TUNNEL_ID);
    }

    // 从1开始，  clientNum
    public Integer getNextClientNumIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.CLIENT_NUM);
        return incrementNumber.intValue();
    }

    public void resetClientNumIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.CLIENT_NUM);
    }
    // 从1开始，  时隙编号
    public Integer getNextTimeSlotIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.TIME_SLOT);
        return 100+incrementNumber.intValue();
    }

    public void resetTimeSlotIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.TIME_SLOT);
    }
    // 从1开始，  路由表端口-》FgClient
    public Integer getNextFgClientIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.FG_CLIENT);
        return incrementNumber.intValue();
    }

    public void resetFgClientIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.FG_CLIENT);
    }

    public String getNextVcIdIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.VC_ID);
        return incrementNumber + "";
    }

    public void resetVcIdIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.VC_ID);
    }

    public String getNextMeNameIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.ME_NAME);
        return String.valueOf(incrementNumber);
    }

    public void resetMeNameIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.ME_NAME);
    }

    public String getNextOamIdIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.OAM_ID);
        return String.valueOf(incrementNumber);
    }

    public void resetOamIdIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.OAM_ID);
    }

    public String getNextBundleIdIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.BUNDLE_ID);
        return String.valueOf(incrementNumber);
    }

    public void resetBundleIdIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.BUNDLE_ID);
    }
    public String getNextLspIdIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.LSP_ID);
        return String.valueOf(incrementNumber);
    }

    public void resetLspIdIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.LSP_ID);
    }
    public String getNextMtnCrossIdIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.MTN_CORSS);
        return String.valueOf(incrementNumber);
    }

    public void resetMtnCrossIdIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.MTN_CORSS);
    }
    public String getNextMtnFgClientNumIdIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.MTN_FGCLIENT_NUM);
        return String.valueOf(incrementNumber);
    }

    public void resetMtnMtnFgClientNumIdIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.MTN_FGCLIENT_NUM);
    }
    public String getNextSingleVethIdIncrementNumber() {
        Long incrementNumber = redisCache.increment(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.SINGLE_VETH);
        return String.valueOf(incrementNumber);
    }

    public void resetSingleVethIdIncrementNumber() {
        redisCache.deleteObject(CacheConstants.REDIS_GLOBAL_INCR + CacheConstants.SINGLE_VETH);
    }
}